package com.ruanyun.modular.admin.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.consts.PublicCommon;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.admin.entity.TExhaustOutlet;
import com.ruanyun.modular.admin.mapper.PollutantsMapper;
import com.ruanyun.modular.admin.mapper.TCommitReportMapper;
import com.ruanyun.modular.admin.model.params.PollutantsParam;
import com.ruanyun.modular.admin.model.result.PollutantsResult;
import com.ruanyun.modular.admin.model.result.TExhaustEquipmentResult;
import com.ruanyun.sys.core.shiro.ShiroKit;
import com.ruanyun.modular.admin.mapper.TExhaustOutletMapper;
import com.ruanyun.modular.admin.model.params.TExhaustOutletParam;
import com.ruanyun.modular.admin.model.result.TExhaustOutletResult;
import  com.ruanyun.modular.admin.service.TExhaustOutletService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 废气排放口 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-04-17
 */
@Service
public class TExhaustOutletServiceImpl extends ServiceImpl<TExhaustOutletMapper, TExhaustOutlet> implements TExhaustOutletService {
    @Autowired
    private TCommitReportMapper commitReportMapper;

     @Autowired
     private PollutantsMapper pollutantsMapper;
    @Autowired
    private UserService userService;


    @Override
    public ResponseData add(TExhaustOutletParam param){
        TExhaustOutlet entity = getEntity(param);
        if(entity.getCompanyId() ==null ){
            return  ResponseData.error("未选择企业所属企业 清先查看企业信息是否填写完整");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        try {
            this.baseMapper.insert(entity);
        } catch (DuplicateKeyException e){
            return ResponseData.error("请选择其他企业");
        }
        return ResponseData.success();
    }

    @Override
    public void delete(TExhaustOutletParam param){
        this.removeById(getKey(param));
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @Override
    public ResponseData updateDelFlagById(Long id) {
        TExhaustOutlet entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该废气排放口不存在");
        }


        entity.setDelFlag(PublicCommon.DEL_FLAG_DELETE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(TExhaustOutletParam param){
        TExhaustOutlet oldEntity = getOldEntity(param);
         if(ToolUtil.isEmpty(param.getIsInside()) ){
             param.setIsInside(0);
         }
//        BigDecimal bg = new BigDecimal(param.getEmissionLimit());
//        param.setEmissionLimit(bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
        TExhaustOutlet newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);

        Integer count = commitReportMapper.getIsCommit(oldEntity.getCompanyId());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法修改");
        }
        int result = this.baseMapper.updateById(newEntity);
        if(result == 0){
        return ResponseData.error("修改失败");
        }
        return ResponseData.success();
    }

    @Override
    public TExhaustOutletResult findBySpec(TExhaustOutletParam param){
        return null;
    }

    @Override
    public List<TExhaustOutletResult> findListBySpec(TExhaustOutletParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TExhaustOutletParam param){
        /*判断当前用户是否为管理员*/
        boolean flag = ShiroKit.isAdmin();
        if (flag != true){
            param.setCompanyId(userService.getCid());
        }
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList1(pageContext, param);
         List<TExhaustOutletResult> results = page.getRecords();
            //污染物拼接

        String pollutantsType = null;
        String pollutantsName = null;
        String emissionStandards = null;
        String emissionLimit = null;

        for (TExhaustOutletResult result : results) {
                   PollutantsResult polluTantsInfo = baseMapper.getPolluTantsInfo(result.getId());
                    if(polluTantsInfo != null){
                        pollutantsType =  polluTantsInfo.getPollutantsType();
                        pollutantsName = polluTantsInfo.getPollutantsName();
                        emissionStandards = polluTantsInfo.getEmissionStandards();
                        emissionLimit = String.valueOf(polluTantsInfo.getEmissionLimits());
                        result.setPollutantsType(pollutantsType);
                        result.setPollutantsName(pollutantsName);
                        result.setEmissionStandards(emissionStandards);
                        result.setEmissionLimits(emissionLimit);
                    }

        }
         page.setRecords(results);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TExhaustOutletParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TExhaustOutlet getOldEntity(TExhaustOutletParam param) {
        return this.getById(getKey(param));
    }

    private TExhaustOutlet getEntity(TExhaustOutletParam param) {
        TExhaustOutlet entity = new TExhaustOutlet();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
        * 启用
        *
        * @param id
        * @return
        */
    @Override
    public ResponseData enableById(Long id) {
        TExhaustOutlet entity = this.baseMapper.selectById(id);
        if (entity == null) {
        return ResponseData.error("该废气排放口不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("启用失败");
        }
        return ResponseData.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @Override
    public ResponseData disableById(Long id) {
        TExhaustOutlet entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该废气排放口不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("禁用失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData addPollutants(PollutantsParam pollutantsParam) {
        pollutantsParam.setDelFlag(1);
        pollutantsMapper.addPollutants(pollutantsParam);
        return ResponseData.success();
    }

    @Override
    public LayuiPageInfo pollutantsList(PollutantsParam pollutantsParam) {

        Page pageContext = getPageContext();
        IPage page = this.pollutantsMapper.customPageList1(pageContext, pollutantsParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public PollutantsResult pollutantsDetail(Integer id) {

        return pollutantsMapper.pollutantsDetail(id);
    }

    @Override
    public ResponseData updatePollutants(PollutantsParam pollutantsParam) {

        Integer pid = pollutantsParam.getId();
        TExhaustOutletResult exhaustOutletResult  = baseMapper.getCidByPid(pid);
            if(exhaustOutletResult != null){
                Integer count = commitReportMapper.getIsCommit(exhaustOutletResult.getCompanyId());
                if(count > 0 ){
                    return  ResponseData.error("该条信息已提交报告无法修改");
                }
            } else {
                return ResponseData.error("未知错误，请联系管理员");
            }
        int result = pollutantsMapper.updatePollutants(pollutantsParam);
         if(result > 0){
             return  ResponseData.success("更新成功");
         } else {
             return  ResponseData.error("更新失败");
         }
    }

    @Override
    public ResponseData delP(Integer id) {

        int result  =  pollutantsMapper.delP(id);
         if(result >0){
             return  ResponseData.success();
         }else {
             return ResponseData.error("删除失败");
         }
    }

    @Override
    public Page<TExhaustOutletResult> getExhaustPage(Integer companyId) {
        Page pageContext = getPageContext();
        Page<TExhaustOutletResult>  exhaustEquipmentPage =  baseMapper.getExhaustPage(pageContext,companyId);
        List<TExhaustOutletResult> results = exhaustEquipmentPage.getRecords();
        //污染物拼接

        String pollutantsType = null;
        String pollutantsName = null;
        String emissionStandards = null;
        String emissionLimit = null;

        for (TExhaustOutletResult result : results) {
            PollutantsResult polluTantsInfo = baseMapper.getPolluTantsInfo(result.getId());
            if(polluTantsInfo != null){
                pollutantsType =  polluTantsInfo.getPollutantsType();
                pollutantsName = polluTantsInfo.getPollutantsName();
                emissionStandards = polluTantsInfo.getEmissionStandards();
                emissionLimit = String.valueOf(polluTantsInfo.getEmissionLimits());
                result.setPollutantsTypeArray(pollutantsType.split(","));
                result.setPollutantsNameArray(pollutantsName.split(","));
                result.setEmissionStandardsArray(emissionStandards.split(","));
                result.setEmissionLimitArray(emissionLimit.split(","));
            }

        }
        exhaustEquipmentPage.setRecords(results);
        return exhaustEquipmentPage;
    }

}
